import { observable, action } from 'mobx';
import { getModifyLog } from '../apis/api';

export default class Store{

    @observable loading = false;
    baseParams = {
        ismonitor: 0,
        urger: 0
    };
    @observable isAll = false;
    @observable showDialog = false;
    @observable nodeLogs = [];
    closeDialogFun = null;

    constructor(){
        this.appendBaseParams = this.appendBaseParams;
        this.setCloseDialogFun = this.setCloseDialogFun;
        this.controlDialog = this.controlDialog;
        this.switchLogRange = this.switchLogRange;
        this.getDatas = this.getDatas;
    }

    @action
    appendBaseParams = (params={}, defShow=false) => {
        this.baseParams = {...this.baseParams, ...params};
        if(defShow)
            this.controlDialog(true);
    }

    setCloseDialogFun = (fun) => {
        this.closeDialogFun = fun;
    }

    controlDialog = (bool) => {
        this.showDialog = bool;
        if(bool)
            this.getDatas();
        else{    //关闭窗口即注销数据
            this.nodeLogs = [];
            this.isAll = false;
            typeof this.closeDialogFun === "function" && this.closeDialogFun();
        }
    }

    switchLogRange = (isAll=false) => {
        this.isAll = isAll;
        this.getDatas();
    }

    getDatas = () => {
        this.loading = true;
        getModifyLog({...this.baseParams, ...{isAll: this.isAll ? 1 : 0}}).then((data) => {
            this.loading = false;
            this.nodeLogs = data.nodeLogs;
        });
    }
}